package com.sgcc.sx.pmos.e_sx_mall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.sx.pmos.arouter.mall.MallArouterPath;
import com.sgcc.sx.pmos.common.adapter.HomeTabPagerAdapter;
import com.sgcc.sx.pmos.common.base.MvpBaseActivity;
import com.sgcc.sx.pmos.common.bean.EventCenter;
import com.sgcc.sx.pmos.common.bean.PackageBean;
import com.sgcc.sx.pmos.common.bean.PowMonth;
import com.sgcc.sx.pmos.common.utils.ArguConstant;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.e_sx_mall.bean.DataSelect;
import com.sgcc.sx.pmos.e_sx_mall.bean.OrderCheckResutBean;
import com.sgcc.sx.pmos.e_sx_mall.contract.BargainPlaceOrderContract;
import com.sgcc.sx.pmos.e_sx_mall.fragment.AuthorizeFragment;
import com.sgcc.sx.pmos.e_sx_mall.fragment.OrderDateChioceFragment;
import com.sgcc.sx.pmos.e_sx_mall.fragment.PowSetTotalFragment;
import com.sgcc.sx.pmos.e_sx_mall.fragment.PowerSetFragment;
import com.sgcc.sx.pmos.e_sx_mall.presenter.BargainPlaceOrderPresenter;
import com.sgcc.sx.pmos.e_sx_mall.widget.NoScrollViewPager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


/**
 * 套餐详情
 * <p>
 * android锚点定位 Android 实现锚点定位思路详解
 * http://www.qb5200.com/article/353138.html
 */
@Route(path = MallArouterPath.MALL_PLACEORDER_BARGAIN)
public class BargainPlaceOrderActivity extends MvpBaseActivity<BargainPlaceOrderPresenter> implements BargainPlaceOrderContract.View {

    private TitleBar mTitleBar;
    private List<Fragment> fragments = new ArrayList<>();
    private HomeTabPagerAdapter homeTabPagerAdapter;
    private NoScrollViewPager mVpContent;

    private TextView txtDate1,txtDate2,txtDate3;
    private ImageView imgDate1,imgDate2,imgDate3;
    private TextView line1,line2;
    private LinearLayout pow_set_linear;

    private Intent intent;
    private DataSelect dataSelect;
    List<PowMonth> mData;
    OrderDateChioceFragment orderDateChioceFragment = new OrderDateChioceFragment();
    PowerSetFragment powerSetFragment = new PowerSetFragment();
    PowSetTotalFragment powSetTotalFragment = new PowSetTotalFragment();
    AuthorizeFragment authorizeFragment = new AuthorizeFragment();
    PackageBean aPackage;
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_bargain_placeorder;
    }

    @Override
    protected BargainPlaceOrderPresenter createPresenter() {
        return new BargainPlaceOrderPresenter();
    }

    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Override
    public void onEventCallBack(EventCenter eventCenter) {
        super.onEventCallBack(eventCenter);
        switch (eventCenter.getEventCode()) {
            case ArguConstant.PLACEORDER_GO_POWERSET:
                mVpContent.setCurrentItem(1);
                changePage(1);
                break;
            case ArguConstant.PLACEORDER_GO_CONTRACT:
                if(getOrderType(aPackage)==3){
                    mVpContent.setCurrentItem(1);
                    changePage(1);
                    mData = powSetTotalFragment.getPowMonthList();
                }else{
                    mVpContent.setCurrentItem(2);
                    changePage(2);
                    mData = powerSetFragment.getPowMonthList();
                }
                break;
            case ArguConstant.PLACEORDER_GO_COMMIT:
                mPresenter.postPlaceOrder(aPackage.getCombId(), dataSelect.getStartdata(), dataSelect.getEnddata(), mData,aPackage.getMembersSdId(),authorizeFragment.getmContent());
                break;
        }
    }

    @Override
    protected void initViews() {
        super.initViews();
        intent = getIntent();
        pow_set_linear= findViewById(R.id.pow_set_linear);
        txtDate1=findViewById(R.id.text_data1);
        txtDate2=findViewById(R.id.text_data2);
        txtDate3=findViewById(R.id.text_data3);

        imgDate1=findViewById(R.id.img_data1);
        imgDate2=findViewById(R.id.img_data2);
        imgDate3=findViewById(R.id.img_data3);

        line1=findViewById(R.id.line1);
        line2=findViewById(R.id.line2);


        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.setTitle("议价下单");
        mTitleBar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMainActivity();
            }
        });
        mVpContent = (NoScrollViewPager) findViewById(R.id.vp_content);

        aPackage = (PackageBean) intent.getSerializableExtra("package");
        Bundle bundle = new Bundle();
        bundle.putSerializable("package", aPackage);
        orderDateChioceFragment.setArguments(bundle);
        orderDateChioceFragment.setOnTimeSelectListener(new OrderDateChioceFragment.OnTimeSelectListener() {
            @Override
            public void timeSelect(DataSelect dataSelect) {
                BargainPlaceOrderActivity.this.dataSelect = dataSelect;
                mVpContent.setCurrentItem(1);
                changePage(1);
                if(getOrderType(aPackage)==1){
                    powerSetFragment.setSelectData(dataSelect);
                }else if(getOrderType(aPackage)==2){
                    powSetTotalFragment.setSelectData(dataSelect);
                }
            }
        });


        fragments.add(orderDateChioceFragment);
        switch (getOrderType(aPackage)){
            case 1:
                fragments.add(powerSetFragment);
                break;
            case 2:
                fragments.add(powSetTotalFragment);
                break;
            default:
                pow_set_linear.setVisibility(View.GONE);
        }
        fragments.add(authorizeFragment);
        homeTabPagerAdapter = new HomeTabPagerAdapter(getSupportFragmentManager(), fragments);
//        homeTabPagerAdapter.setmBottomTabNames(titleList);
        mVpContent.setOffscreenPageLimit(2);
        mVpContent.setAdapter(homeTabPagerAdapter);
        mVpContent.setCurrentItem(0);
//        mStlContent.setViewPager(mVpContent);
        mVpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                changePage(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void changePage(int pageNum) {
        setDefuleTitleColor();
        switch (pageNum) {
            case 0:
                txtDate1.setTextColor(0xff0092FF);
                imgDate1.setImageResource(R.mipmap.circular);
                break;
            case 1:
                txtDate1.setTextColor(0xff0092FF);
                imgDate1.setImageResource(R.mipmap.circular);
                txtDate2.setTextColor(0xff0092FF);
                imgDate2.setImageResource(R.mipmap.circular);
                line1.setTextColor(0xff0092FF);
                break;
            case 2:
                txtDate1.setTextColor(0xff0092FF);
                imgDate1.setImageResource(R.mipmap.circular);
                txtDate2.setTextColor(0xff0092FF);
                imgDate2.setImageResource(R.mipmap.circular);
                txtDate3.setTextColor(0xff0092FF);
                imgDate3.setImageResource(R.mipmap.circular);
                line1.setTextColor(0xff0092FF);
                line2.setTextColor(0xff0092FF);
                break;
        }
    }
   private void  setDefuleTitleColor(){
       txtDate1.setTextColor(0xff999999);
       txtDate2.setTextColor(0xff999999);
       txtDate3.setTextColor(0xff999999);

       imgDate1.setImageResource(R.mipmap.circular_pre);
       imgDate2.setImageResource(R.mipmap.circular_pre);
       imgDate3.setImageResource(R.mipmap.circular_pre);

       line1.setTextColor(0xff999999);
       line2.setTextColor(0xff999999);


    }


    @Override
    public void placeOrderSuccess(OrderCheckResutBean response) {
        if (response.getCode() == 200) {
            ToastUtils.showShort("提交成功");
            ARouter.getInstance()
                    .build(MallArouterPath.MALL_LACEORDER_SUCCESS)
                    .withBoolean("isNow",false)
                    .navigation();
            EventBus.getDefault().post(new EventCenter<>(ArguConstant.REFRESH_CONTRACT));
            this.finish();
        }
    }
    @Override
    public void placeOrderFailed() {
        ToastUtils.showShort("下单失败");
    }


    /**
     * 根据套餐详情返回页面类型
     * 1分电量、分时   2 分电量、不分时    3不分电量
     * @param bean
     * @return
     */
    private int getOrderType(PackageBean bean) {
        int orderType = 0;//1分电量、分时   2 分电量、不分时    3不分电量
        if(TextUtils.equals("1",bean.getpNum())){
            //如果为单一套餐
            switch (bean.getMainType()){
                case "01":
                    orderType=1;
                    break;
                case "02":
                    orderType=3;
                    break;
                case "03":
                    orderType=2;
                    break;
                case "04":
                    orderType=3;
                    break;
            }
        }else if(TextUtils.equals("2",bean.getpNum())){
            //如果为组合套餐  联合判断
            switch (bean.getMainType()){
                case "01":
                    orderType=1;
                    break;
                case "02":
                    switch (bean.getFloType()){
                        case "01":
                            orderType=1;
                            break;
                        case "02":
                            orderType=3;
                            break;
                        case "03":
                            orderType=1;
                            break;
                        case "04":
                            orderType=3;
                            break;
                    }
                    break;
                case "03":
                    switch (bean.getFloType()){
                        case "01":
                            orderType=1;
                            break;
                        case "02":
                            orderType=1;
                            break;
                        case "03":
                            orderType=2;
                            break;
                        case "04":
                            orderType=2;
                            break;
                    }
                    break;
                case "04":
                    switch (bean.getFloType()){
                        case "01":
                            orderType=1;
                            break;
                        case "02":
                            orderType=3;
                            break;
                        case "03":
                            orderType=2;
                            break;
                        case "04":
                            orderType=3;
                            break;
                    }
                    break;
            }
        }



        return orderType;

    }
}
